<?php

	if($_GET['operation']=="add" or $_GET['operation']=="edit")
	{
		if($_POST['act'])
		{	
			unset($_GET['operation']);
			unset($_GET['s']);
			unset($_GET['m']);
			$param = $_POST;
			$param['add_time'] = time();
			$param['statu'] = isset($_POST['statu']) ? $_POST['statu'] : 0;
			$param['provinceid'] = !empty(getdistrictid($_SESSION['province'])) ? getdistrictid($_SESSION['province']) : 0;
			$param['cityid'] = !empty(getdistrictid($_SESSION['city'])) ? getdistrictid($_SESSION['city']) : 0;
			$param['areaid'] = !empty(getdistrictid($_SESSION['area'])) ? getdistrictid($_SESSION['area']) : 0;

			//添加
			if($_POST["act"]=='save')
			{
				if(empty($param['cat_name'])){
					admin_msg("module.php?m=spot&s=spot_cat.php","分类名称不能为空");
					return false;
				}
				$db->query("insert into ".SPOTCAT." (`cat_name`,`statu`,`add_time`,`provinceid`,`cityid`,`areaid`) values
				('{$param['cat_name']}','{$param['statu']}','{$param['add_time']}','{$param['provinceid']}','{$param['cityid']}',{$param['areaid']}'')");
				$last_id = $db->lastid();
				if($last_id){
					admin_msg("module.php?m=spot&s=spot_cat.php","添加成功");
					return false;
				} else {
					admin_msg("module.php?m=spot&s=spot_cat.php","添加失败");
					return false;
				}
			}
			//修改
			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				$sql = "update ".SPOTCAT." set cat_name = '{$param['cat_name']}',statu = '{$param['statu']}' where id = '{$param['id']}'   ";
				if($db->query($sql)){
					msg("?m=spot&s=spot_cat.php");
				} else {
					msg("?m=spot&s=spot_cat.php",'修改失败');
				}

			}


		}

		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".SPOTCAT." where id='$_GET[editid]'";
			$db->query($sql);
			$re=$db->fetchRow();
			$tpl->assign("re",$re);
		}

	}
	else
	{
		if($_GET['delid'])
		{
			$sql="delete from ".SPOTCAT."  where id like '$_GET[delid]%' ";
			$db->query($sql);
			msg("?m=spot&s=spot_cat.php");
		}
		if($_SESSION['province']){
			$psql = " and provinceid = ".getdistrictid($_SESSION['province']);
		}
		if($_SESSION['city']){
			$psql .= " and cityid = ".getdistrictid($_SESSION['city']);
		}
		if($_SESSION['area']){
			$psql .= " and areaid = ".getdistrictid($_SESSION['area']);
		}
		$sql = "select * from ".SPOTCAT." where 1 $psql order by id desc";
		$db->query($sql);
		$de = $db->getRows();

	}
	$tpl->assign("de",$de);
	$tpl->display("spot_cat.htm");
?>